import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Query,
  Delete,
  ParseIntPipe,
  UseInterceptors,
  ClassSerializerInterceptor,
} from "@nestjs/common";
import { UsersService } from "./users.service";
import { UserDto, UpdateUserDto, SearchUserDto } from "./dto/user.dto";
import { ApiOperation, ApiResponse } from "@nestjs/swagger";

@UseInterceptors(ClassSerializerInterceptor)
@Controller("users")
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Post()
  @ApiOperation({ summary: "创建用户" })
  create(@Body() createUser: UserDto) {
    // console.log('create', createUser);
    return this.usersService.create(createUser);
  }

  @Get()
  @ApiOperation({ summary: "查询所有用户" })
  @ApiResponse({ status: 200, description: "成功返回用户列表" })
  findAll(@Query() query: SearchUserDto) {
    // const { page, pageSize } = query;
    return this.usersService.findAll(query);
  }

  @Get(":id")
  @ApiOperation({ summary: "根据ID查询用户" })
  @ApiResponse({ status: 200, description: "成功返回用户信息" })
  findOne(@Param("id", ParseIntPipe) id: number) {
    return this.usersService.findOne(id);
  }

  @Patch("/:id")
  @ApiOperation({ summary: "更新用户信息" })
  update(
    @Param("id", ParseIntPipe) id: number,
    @Body() updateUserDto: UpdateUserDto,
  ) {
    return this.usersService.update(id, updateUserDto);
  }

  @Delete(":id")
  @ApiOperation({ summary: "删除用户" })
  remove(@Param("id", ParseIntPipe) id: number) {
    return this.usersService.remove(id);
  }
}
